home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 09 - 1993 / 09.06 Jun 93 / Getting Started / Modeless.c next >
Encoding:
C/C++ Source or Header  |  1993-05-03  |  8.7 KB  |  467 lines  |  [TEXT/KAHL]

  1. /****************************************************/
  2. /*                                                    */
  3. /*  Modeless Code                                    */
  4. /*                                                  */
  5. /*    Copyright 1993, Dave Mark, All rights reserved     */
  6. /*    This code is proprietary property of Dave Mark    */
  7. /*    and may not be copied, distributed, etc.        */
  8. /*                                                    */    
  9. /****************************************************/
  10.  
  11. #define kBaseResID            128
  12. #define kAboutALRTid        129
  13. #define kDialogResID        128
  14.  
  15. #define kVisible            true
  16. #define    kMoveToBack            NULL
  17. #define    kMoveToFront        (WindowPtr)-1L
  18. #define kNoGoAway            false
  19. #define kSleep                60L
  20.  
  21. #define kOn                    1
  22. #define kOff                0
  23.  
  24. #define iAfghan                1
  25. #define iElephant            2
  26. #define iSquirrel            3
  27.  
  28. #define kLeftMargin            5
  29. #define kTopMargin            40
  30.  
  31. #define kFirstRadio            1
  32. #define kLastRadio            3
  33.  
  34. #define mApple                kBaseResID
  35. #define iAbout                1
  36.  
  37. #define mFile                kBaseResID+1
  38. #define iSettings            1
  39. #define iQuit                3
  40.  
  41.  
  42. /*************/
  43. /*  Globals  */
  44. /*************/
  45.  
  46. Boolean        gDone;
  47. short        gCurrentPICT = kBaseResID;
  48. DialogPtr    gSettingsDLOG = NULL;
  49. WindowPtr    gFredWindow = NULL;
  50.  
  51.  
  52. /***************/
  53. /*  Functions  */
  54. /***************/
  55.  
  56. void        ToolBoxInit( void );
  57. PicHandle    LoadPICT( short picID );
  58. void        CreateWindow( void );
  59. void        MenuBarInit( void );
  60. void        EventLoop( void );
  61. void        DoEvent( EventRecord *eventPtr );
  62. void        DoDialogEvent( EventRecord *eventPtr );
  63. void        HandleMouseDown( EventRecord *eventPtr );
  64. void        HandleMenuChoice( long menuChoice );
  65. void        HandleAppleChoice( short item );
  66. void        HandleFileChoice( short item );
  67. void        DoUpdate( EventRecord *eventPtr );
  68. void        CreateDialog( void );
  69. void        FlipControl( ControlHandle control );
  70. void        SwitchPICT( void );
  71.     
  72.  
  73. /******************************** main *********/
  74.  
  75. void    main( void )
  76. {
  77.     ToolBoxInit();
  78.     MenuBarInit();
  79.     
  80.     CreateWindow();
  81.     
  82.     EventLoop();
  83. }
  84.  
  85.  
  86. /*********************************** ToolBoxInit */
  87.  
  88. void    ToolBoxInit( void )
  89. {
  90.     InitGraf( &thePort );
  91.     InitFonts();
  92.     InitWindows();
  93.     InitMenus();
  94.     TEInit();
  95.     InitDialogs( NULL );
  96.     InitCursor();
  97. }
  98.  
  99.  
  100. /******************************** LoadPICT *********/
  101.  
  102. PicHandle    LoadPICT( short picID )
  103. {
  104.     PicHandle    pic;
  105.     
  106.     pic = GetPicture( picID );
  107.     
  108.     if ( pic == NULL )
  109.     {
  110.         SysBeep( 10 );    /*  Couldn't load the PICT resource!!!  */
  111.         ExitToShell();
  112.     }
  113.     return( pic );
  114. }
  115.  
  116.  
  117. /******************************** CreateWindow *********/
  118.  
  119. void    CreateWindow( void )
  120. {
  121.     PicHandle    pic;
  122.     Rect        r;
  123.     
  124.     pic = LoadPICT( gCurrentPICT );
  125.     
  126.     r = (**pic).picFrame;
  127.     
  128.     OffsetRect( &r, kLeftMargin - r.left,
  129.                     kTopMargin - r.top );
  130.     
  131.     gFredWindow = NewWindow( NULL, &r, "\pMy Pet Fred", kVisible,
  132.             noGrowDocProc, kMoveToBack, kNoGoAway, 0L );
  133.     
  134.     if ( gFredWindow == NULL )
  135.     {
  136.         SysBeep( 10 );    /*  Couldn't load the WIND resource!!!  */
  137.         ExitToShell();
  138.     }
  139.     
  140.     ShowWindow( gFredWindow );
  141.     SetPort( gFredWindow );
  142. }
  143.  
  144.  
  145. /****************** MenuBarInit ***********************/
  146.  
  147. void    MenuBarInit( void )
  148. {
  149.     Handle            menuBar;
  150.     MenuHandle        menu;
  151.     
  152.     menuBar = GetNewMBar( kBaseResID );
  153.     SetMenuBar( menuBar );
  154.  
  155.     menu = GetMHandle( mApple );
  156.     AddResMenu( menu, 'DRVR' );
  157.     
  158.     DrawMenuBar();
  159. }
  160.  
  161.  
  162. /******************************** EventLoop *********/
  163.  
  164. void    EventLoop( void )
  165. {        
  166.     EventRecord        event;
  167.     
  168.     gDone = false;
  169.     while ( gDone == false )
  170.     {
  171.         if ( WaitNextEvent( everyEvent, &event, kSleep, NULL ) )
  172.             DoEvent( &event );
  173.     }
  174. }
  175.  
  176.  
  177. /************************************* DoEvent     */
  178.  
  179. void    DoEvent( EventRecord *eventPtr )
  180. {
  181.     char        theChar;
  182.     
  183.     if ( IsDialogEvent( eventPtr ) )
  184.     {
  185.         DoDialogEvent( eventPtr );
  186.     }
  187.     else
  188.     {
  189.         switch ( eventPtr->what )
  190.         {
  191.             case mouseDown: 
  192.                 HandleMouseDown( eventPtr );
  193.                 break;
  194.             case keyDown:
  195.             case autoKey:
  196.                 theChar = eventPtr->message & charCodeMask;
  197.                 if ( (eventPtr->modifiers & cmdKey) != 0 ) 
  198.                     HandleMenuChoice( MenuKey( theChar ) );
  199.                 break;
  200.             case updateEvt:
  201.                 DoUpdate( eventPtr );
  202.                 break;
  203.         }
  204.     }
  205. }
  206.  
  207.  
  208. /************************************* DoDialogEvent     */
  209.  
  210. void    DoDialogEvent( EventRecord *eventPtr )
  211. {
  212.     short        itemHit;
  213.     short        itemType;
  214.     Handle        itemHandle;
  215.     Rect        itemRect;
  216.     short        curRadioButton, i;
  217.     char        theChar;
  218.     Boolean        becomingActive;
  219.     MenuHandle    menu;
  220.     DialogPtr    dialog;
  221.     
  222.     menu = GetMHandle( mFile );
  223.  
  224.     switch ( eventPtr->what )
  225.     {
  226.         case keyDown:
  227.         case autoKey:
  228.             theChar = eventPtr->message & charCodeMask;
  229.             if ( (eventPtr->modifiers & cmdKey) != 0 ) 
  230.                 HandleMenuChoice( MenuKey( theChar ) );
  231.             break;
  232.         case activateEvt:
  233.             becomingActive = ( (eventPtr->modifiers & activeFlag) == activeFlag );
  234.             
  235.             if ( becomingActive )
  236.             {
  237.                 for ( i=kFirstRadio; i<=kLastRadio; i++ )
  238.                 {
  239.                     GetDItem( gSettingsDLOG, i, &itemType, &itemHandle, &itemRect );
  240.                     HiliteControl( (ControlHandle)itemHandle, 0 );
  241.                 }
  242.                 DisableItem( menu, iSettings );
  243.             }
  244.             else
  245.             {
  246.                 for ( i=kFirstRadio; i<=kLastRadio; i++ )
  247.                 {
  248.                     GetDItem( gSettingsDLOG, i, &itemType, &itemHandle, &itemRect );
  249.                     HiliteControl( (ControlHandle)itemHandle, 255 );
  250.                 }
  251.                 EnableItem( menu, iSettings );
  252.             }
  253.             break;
  254.     }
  255.         
  256.     if ( DialogSelect( eventPtr, &dialog, &itemHit ) )
  257.     {
  258.         switch ( itemHit )
  259.         {
  260.             case iAfghan:
  261.             case iElephant:
  262.             case iSquirrel:
  263.                 curRadioButton = gCurrentPICT - kBaseResID + kFirstRadio;
  264.     
  265.                 if ( curRadioButton != itemHit )
  266.                 {
  267.                     GetDItem( dialog, curRadioButton, &itemType,
  268.                             &itemHandle, &itemRect );
  269.                     FlipControl( (ControlHandle)itemHandle );
  270.                     
  271.                     GetDItem( dialog, itemHit, &itemType,
  272.                             &itemHandle, &itemRect );
  273.                     FlipControl( (ControlHandle)itemHandle );
  274.                     
  275.                     curRadioButton = itemHit;
  276.                     
  277.                     if ( gCurrentPICT != curRadioButton +
  278.                             kBaseResID - kFirstRadio )
  279.                     {
  280.                         gCurrentPICT = curRadioButton +
  281.                                 kBaseResID - kFirstRadio;
  282.                         SwitchPICT();
  283.                     }
  284.                 }
  285.                 break;
  286.         }
  287.     }
  288. }
  289.  
  290.  
  291. /************************************* HandleMouseDown */
  292.  
  293. void    HandleMouseDown( EventRecord *eventPtr )
  294. {
  295.     WindowPtr        window;
  296.     short            thePart;
  297.     long            menuChoice;
  298.     GrafPtr            oldPort;
  299.     long            windSize;
  300.     Rect            growRect;
  301.     MenuHandle        menu;
  302.     
  303.     thePart = FindWindow( eventPtr->where, &window );
  304.     
  305.     switch ( thePart )
  306.     {
  307.         case inMenuBar:
  308.             menuChoice = MenuSelect( eventPtr->where );
  309.             HandleMenuChoice( menuChoice );
  310.             break;
  311.         case inSysWindow : 
  312.             SystemClick( eventPtr, window );
  313.             break;
  314.         case inContent:
  315.             SelectWindow( window );
  316.             break;
  317.         case inDrag : 
  318.             DragWindow( window, eventPtr->where, &screenBits.bounds );
  319.             break;
  320.         case inGoAway:
  321.             if ( TrackGoAway( window, eventPtr->where ) )
  322.                 if ( window == gSettingsDLOG )
  323.                 {
  324.                     HideWindow( window );
  325.                     menu = GetMHandle( mFile );
  326.                     EnableItem( menu, iSettings );
  327.                 }
  328.             break;
  329.     }
  330. }
  331.  
  332.  
  333. /****************** HandleMenuChoice ***********************/
  334.  
  335. void    HandleMenuChoice( long menuChoice )
  336. {
  337.     short    menu;
  338.     short    item;
  339.     
  340.     if ( menuChoice != 0 )
  341.     {
  342.         menu = HiWord( menuChoice );
  343.         item = LoWord( menuChoice );
  344.         
  345.         switch ( menu )
  346.         {
  347.             case mApple:
  348.                 HandleAppleChoice( item );
  349.                 break;
  350.             case mFile:
  351.                 HandleFileChoice( item );
  352.                 break;
  353.         }
  354.         HiliteMenu( 0 );
  355.     }
  356. }
  357.  
  358.  
  359. /****************** HandleAppleChoice ***********************/
  360.  
  361. void    HandleAppleChoice( short item )
  362. {
  363.     MenuHandle    appleMenu;
  364.     Str255        accName;
  365.     short        accNumber;
  366.     
  367.     switch ( item )
  368.     {
  369.         case iAbout:
  370.             NoteAlert( kAboutALRTid, NULL );
  371.             break;
  372.         default:
  373.             appleMenu = GetMHandle( mApple );
  374.             GetItem( appleMenu, item, accName );
  375.             accNumber = OpenDeskAcc( accName );
  376.             break;
  377.     }
  378. }
  379.  
  380.  
  381. /****************** HandleFileChoice ***********************/
  382.  
  383. void    HandleFileChoice( short item )
  384. {
  385.     switch ( item )
  386.     {
  387.         case iSettings:
  388.             if ( gSettingsDLOG == NULL )
  389.                 CreateDialog();
  390.             else
  391.             {
  392.                 ShowWindow( gSettingsDLOG );
  393.                 SelectWindow( gSettingsDLOG );
  394.             }
  395.             break;
  396.         case iQuit:
  397.             gDone = true;
  398.             break;
  399.     }
  400. }
  401.  
  402.  
  403. /************************************* DoUpdate     */
  404.  
  405. void    DoUpdate( EventRecord *eventPtr )
  406. {
  407.     PicHandle    pic;
  408.     WindowPtr    window;
  409.     Rect        r;
  410.     
  411.     window = (WindowPtr)eventPtr->message;
  412.     
  413.     pic = LoadPICT( gCurrentPICT );
  414.     
  415.     SetPort( window );
  416.     
  417.     BeginUpdate( window );
  418.     
  419.     r = window->portRect;
  420.     DrawPicture( pic, &r );
  421.     
  422.     EndUpdate( window );
  423. }
  424.  
  425.  
  426. /************************************* CreateDialog     */
  427.  
  428. void    CreateDialog( void )
  429. {
  430.     short        itemType;
  431.     Handle        itemHandle;
  432.     Rect        itemRect;
  433.     short        curRadioButton;
  434.  
  435.     gSettingsDLOG = GetNewDialog( kDialogResID, NULL, kMoveToFront );
  436.  
  437.     if ( gSettingsDLOG == NULL )
  438.     {
  439.         SysBeep( 10 );    /*  Couldn't load the DLOG resource!!!  */
  440.         ExitToShell();
  441.     }
  442.     
  443.     ShowWindow( gSettingsDLOG );
  444.     SetPort( gSettingsDLOG );
  445.     
  446.     curRadioButton = gCurrentPICT - kBaseResID + kFirstRadio;
  447.     GetDItem( gSettingsDLOG, curRadioButton, &itemType, &itemHandle, &itemRect );
  448.     SetCtlValue( (ControlHandle)itemHandle, kOn );
  449. }
  450.  
  451.  
  452. /************************************* FlipControl     */
  453.  
  454. void    FlipControl( ControlHandle control )
  455. {
  456.     SetCtlValue( control, ! GetCtlValue( control ) );
  457. }
  458.  
  459.  
  460. /************************************* SwitchPICT     */
  461.  
  462. void    SwitchPICT( void )
  463. {
  464.     DisposeWindow( gFredWindow );
  465.     
  466.     CreateWindow();
  467. }